package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.SensitiveMapper;
import com.heima.admin.service.SensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper, AdSensitive> implements SensitiveService {

    /**
     * 查询敏感词
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(SensitiveDTO dto) {
        // 1. 参数校验代码
        dto.checkParam();
        // 2. 分页查询
        // 2.1 封装分页查询条件
        Page<AdSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        // 2.2 其他查询条件
        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())) {  //敏感词名字不为空
            queryWrapper.like(AdSensitive::getSensitives, dto.getName());
        }
        // 2.3 执行查询操作   参数1: 分页条件  参数2： 查询条件
        IPage<AdSensitive> pageResult = page(page, queryWrapper);
        // 封装成 PageResponseResult结果返回
        return new PageResponseResult(dto.getPage(), dto.getSize(),
                pageResult.getTotal(), pageResult.getRecords());
    }


    /**
     * 添加敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        // 1.校验参数
        String sensitives = adSensitive.getSensitives();
        if (adSensitive == null || StringUtils.isBlank(sensitives)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词不能为空");
        }
        // 2判断频道是否存在
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "敏感词已经存在");
        }
        //3.新增
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        // 4.返回结果
        return ResponseResult.okResult();
    }

    /**
     * 修改敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //1 参数检验
        Integer id = adSensitive.getId();
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词id不能为空");
        }
        //  检查数据是否存在
        AdSensitive oldSensitive = getById(adSensitive.getId());
        if (oldSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "敏感词不存在");
        }
        // 校验参数 名字不为空且名字与原来不一致,发生改变，需校验
        String sensitives = adSensitive.getSensitives();
        if (StringUtils.isBlank(sensitives) && !sensitives.equals(oldSensitive.getSensitives())) {
            // 判断是否存在
            int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "敏感词已经存在");
            }
        }
        //3.修改
        updateById(adSensitive);
        // 4.返回结果
        return ResponseResult.okResult();
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        //1 参数检验
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词id不能为空");
        }
        //  检查数据是否存在
        AdSensitive oldSensitive = getById(id);
        if (oldSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "敏感词不存在");
        }
        // 删除敏感词
        removeById(id);
        // 返回结果
        return ResponseResult.okResult();
    }
}
